package com.example.projectjava.controller;

import com.example.projectjava.service.TrolleyService;
import com.example.projectjava.util.JsonResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@Tag(name="购物车接口")
public class TrolleyController {
    @Autowired   //此注解的作用：是spring的基本特性，它将项目中运行过程中的JdbcTemplate,自动创建并实例化传入
    //这种注解是stringboot的一个核心功能，是依赖注入功能
    JdbcTemplate  jdbcTemplate;//自动传入了这个对象

    @Autowired
    TrolleyService trollerservice;

    @ResponseBody //此注解作用：将方法返回值转换为json串返回给客户端
    @GetMapping("/trolleyadd")
    @Operation(summary = "添加到购物车")
    @Parameters({
            @Parameter(name ="userName",description="用户名(不允许为空)",in = ParameterIn.QUERY,required = false),
            @Parameter(name ="goodsName",description="商品名(不允许为空)",in = ParameterIn.QUERY,required = true),
            @Parameter(name ="price",description="商品价格(不允许为空)",in = ParameterIn.QUERY,required = true),
            @Parameter(name ="count",description="商品数量(不允许为空)",in = ParameterIn.QUERY,required = true),
    })
    public  JsonResponse  trolleyadd(String userName,String goodsName,double price,int count){
        JsonResponse jsonRespone = new JsonResponse();
        Double priceObj=price;
        if ( goodsName==null  ||  goodsName.equals("")){
            jsonRespone.setCode(1);
            jsonRespone.setMsg("商品名不能为空");
            //  flag = false;
            return  jsonRespone;
        }
        if ( userName==null  ||  userName.equals("")){
            jsonRespone.setCode(1);
            jsonRespone.setMsg("用户名不能为空");
            //  flag = false;
            return  jsonRespone;
        }
        if ( priceObj.equals("")){
            jsonRespone.setCode(1);
            jsonRespone.setMsg("价格不能为空");
            //  flag = false;
            return  jsonRespone;
        }
        if (count==0){
            jsonRespone.setCode(1);
            jsonRespone.setMsg("商品数量不能为空");
            //  flag = false;
            return  jsonRespone;
        }
        try {
           // Sql

            trollerservice.trolleyadd(userName,goodsName,price,count);
           // jdbcTemplate.update(sql, userName,goodsName,price);
            jsonRespone.setCode(0);
            jsonRespone.setMsg("加入购物车成功");
        }catch (Exception e){
            e.printStackTrace();
            jsonRespone.setCode(1);
            jsonRespone.setMsg("加入购物车失败:"+e.getMessage());
        }

        return  jsonRespone;
    }


   /**修改*/
    @GetMapping("/trolleyUpdate")
    @ResponseBody
    @Operation(summary = "修改购物车信息")
    @Parameters({
            @Parameter(name ="userName",description="用户名(不允许为空)",in = ParameterIn.QUERY,required = false),
            @Parameter(name ="goodsName",description="商品名(不允许为空)",in = ParameterIn.QUERY,required = true),
            @Parameter(name ="price",description="商品价格(不允许为空)",in = ParameterIn.QUERY,required = true),
            @Parameter(name ="count",description="商品数量(不允许为空)",in = ParameterIn.QUERY,required = true),
    })
     public JsonResponse trolleyUpdate(String userName,String goodsName,double price,int count) {
        JsonResponse jsonRespone = new JsonResponse();
        try {
            // Sql

            trollerservice.trolleyUpdate(userName,goodsName,price,count);
            // jdbcTemplate.update(sql, userName,goodsName,price);
            jsonRespone.setCode(0);
            jsonRespone.setMsg("修改商品数量成功");
        }catch (Exception e){
            e.printStackTrace();
            jsonRespone.setCode(1);
            jsonRespone.setMsg("修改商品数量失败:"+e.getMessage());
        }
        return  jsonRespone;
    }
    /**删除*/
    @GetMapping("/trolleydelete")
    @ResponseBody
    @Operation(summary = "删除购物车信息")
    @Parameters({
            @Parameter(name ="userName",description="用户名(不允许为空)",in = ParameterIn.QUERY,required = false),
            @Parameter(name ="goodsName",description="商品名(不允许为空)",in = ParameterIn.QUERY,required = true),
    })
    public JsonResponse deleteTrolley(String userName,String goodsName) {
        JsonResponse jsonResponse = new JsonResponse();
        try {
            trollerservice.deleteTrolley(userName,goodsName);
            jsonResponse.setCode(0);
            jsonResponse.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResponse.setCode(1);
            jsonResponse.setMsg("删除失败:" + e.getMessage());
        }
        return jsonResponse;
    }
    /**查*/
    @GetMapping("/trolleyquery")
    @ResponseBody
    @Operation(summary = "查询购物车信息")
    @Parameters({
            @Parameter(name ="userName",description="用户名(不允许为空)",in = ParameterIn.QUERY,required = false),
    })
    public JsonResponse trolleyquery(String userName) {
        JsonResponse jsonRespone = new JsonResponse();
        List<Map<String,Object>> result =trollerservice.querytrolley(userName);
        if (result.size()==0){
            jsonRespone.setCode(1);
            jsonRespone.setMsg("购物车为空");
            return  jsonRespone;
        }
        jsonRespone.setCode(0);
        jsonRespone.setMsg("查询成功");
        jsonRespone.setData(result);
        return  jsonRespone;
    }
    /**
     * 获得trolley表中的所有数据返回给前端
     */
    @GetMapping("/queryAlltrolley")
    @ResponseBody
    @Operation(summary = "查询所有购物车信息")
    public  JsonResponse queryAllUser(){

        List<Map<String,Object>> result =trollerservice.queryAlltrolley();

        JsonResponse jsonRespone = new JsonResponse();
        jsonRespone.setCode(0);
        jsonRespone.setMsg("查询成功");
        jsonRespone.setData(result);
        return  jsonRespone;
    }
}

